package Engine.Module
{
	import Interface.Module.IModuleBase;
	
	import flash.display.Sprite;
	
	import library.Function.Dict;

	public class ModuleManager
	{
		public static const DEPTH_TOP:int = 1;
		public static const DEPTH_MIDDLE:int = 2;
		public static const DEPTH_BOTTOM:int = 3;
		public static const DEPTH_DEBUG:int = 4;
		
		private var _moduleDict:Dict;
		private var _top:Sprite;
		private var _middle:Sprite;
		private var _bottom:Sprite;
		private var _debug:Sprite;
		
		public function ModuleManager()
		{
			this._top = new Sprite;
			this._middle = new Sprite;
			this._bottom = new Sprite;
			this._debug = new Sprite;
			this._moduleDict = new Dict;
		}
		
		public function insertModule(moduleName:String,module:IModuleBase):void
		{
			this._moduleDict.dictionary[moduleName] = module;
		}
		
		public function removeModel(moduleName:String):void
		{
			this._moduleDict.remove(moduleName);
		}
		
		public function showModule(moduleName:String,depth:int = DEPTH_MIDDLE):void
		{
			var imd:IModuleBase = this.getModule(moduleName);
			if(imd == null)
				return;
			switch(depth)
			{
				case DEPTH_TOP:
					imd.show(this._top);
					break;
				case DEPTH_MIDDLE:
					imd.show(this._middle);
					break;
				case DEPTH_BOTTOM:
					imd.show(this._bottom);
					break;
			}
		}
		
		public function showModuleFromIMD(imd:IModuleBase,depth:int = DEPTH_MIDDLE):void
		{
			if(imd == null)
				return;
			switch(depth)
			{
				case DEPTH_TOP:
					imd.show(this._top);
					break;
				case DEPTH_MIDDLE:
					imd.show(this._middle);
					break;
				case DEPTH_BOTTOM:
					imd.show(this._bottom);
					break;
			}
		}
		
		public function getDepthSprite(depth:int = DEPTH_MIDDLE):Sprite
		{
			var sp:Sprite = null;
			
			switch(depth)
			{
				case DEPTH_TOP:
					sp = this._top;
					break;
				case DEPTH_MIDDLE:
					sp = this._middle;
					break;
				case DEPTH_BOTTOM:
					sp = this._bottom;
					break;
				case DEPTH_DEBUG:
					sp = this._debug;
					break;
			}
			return sp;
		}
		
		
		public function getModule(moduleName:String):IModuleBase
		{
			if(this._moduleDict.has_key(moduleName))
				return this._moduleDict.dictionary[moduleName];
			return null;
		}
		
		public function addToParent(parent:Sprite):void
		{
			parent.addChild(this._bottom);
			parent.addChild(this._middle);
			parent.addChild(this._top);
			parent.addChild(this._debug);
		}
		
		public function clearAllUI():void
		{
			for(var i:* in this._moduleDict.dictionary)
			{
				if(this._moduleDict.dictionary[i] != null)
				{
					this._moduleDict.dictionary[i].dispose();
				}
			}
		}
		
	}
}